rm(list = ls())

## Data source: https://search.gesis.org/research_data/ZA7700
## Note that this figure requires data that we cannot make available publicly

gles <- haven::read_dta('data_private/ZA7700_v1-0-0.dta')

## q47x are perceived party positions on climate change 

climate_pos_vars <- names(gles)[str_detect(names(gles), 'q47')]

gles <- gles %>%
  dplyr::select(climate_pos_vars)


## Code NAs

gles[gles < 0] <- NA

## Reverse-code 

gles_long <- gles %>%
  pivot_longer(cols = everything(),
               names_to = 'party',
               values_to = 'value') %>%
  mutate(value = as.numeric(value),
         party = recode(party,
                        'q47b' = 'CDU/CSU',
                        'q47c' = 'CDU/CSU',
                        'q47d' = 'SPD', 
                        'q47e' = 'FDP', 
                        'q47f' = 'Greens', 
                        'q47g' = 'Left Party',
                        'q47h' = 'AfD')) %>%
  mutate(value = 12 - value)

plot_dat <- gles_long %>%
  group_by(party) %>%
  summarise(party_pos_climate = mean(value, na.rm = T)) %>%
  mutate(party = fct_reorder(party, party_pos_climate))

## 

p1 <- ggplot(plot_dat, aes(x = party, y = party_pos_climate)) + 
  geom_bar(stat = 'identity',
           width = .8) + 
  theme_bw() + 
  labs(x = '',
       y = 'Perceived party position on climate change') + 
  coord_flip()


p1










